package com.hui.xiao.xiaohui.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.hui.xiao.xiaohui.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Administrator on 2016/5/18.
 */
public class SimpleAdapterTest extends Activity {
    private String[] names = new String[]{
            "张龙", "赵虎", "王朝", "马汉"
    };
    private String[] descs = new String[]{
            "高", "矮", "胖", "瘦"};

    private int[] imageIds = new int[]{
            R.drawable.bkch, R.drawable.bkch,
            R.drawable.bkch, R.drawable.aboc
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_adapter);
        List<Map<String, Object>> items = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("header", imageIds[i]);
            item.put("name", names[i]);
            item.put("desc", descs[i]);
            items.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, items,
                R.layout.simple_item, new String[]{"name", "header", "desc"},
                new int[]{R.id.name, R.id.header, R.id.desc});
        ListView list = (ListView) findViewById(R.id.mylist);
        list.setAdapter(simpleAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast toast = Toast.makeText(SimpleAdapterTest.this, names[position] + "被点击", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }
}
